#!/bin/bash
export TTDIR=/usr/local/teamtools

cat <<!
+--------------------------------------------------------------+
|                                                              |
|                      TeamTools installer                     |
|                                                              |
+--------------------------------------------------------------+

!

yum -y install coreutils

echo "(re)installing SubVersion"
yum -y remove subversion
rpm -ivh http://pkgs.repoforge.org/rpmforge-release/rpmforge-release-0.5.3-1.el6.rf.x86_64.rpm
yum install -y --enablerepo=rpmforge-extras subversion mod_dav_svn

echo "Installing dependencies (via yum)"
yum -y install perl-CPAN perl-YAML perl-XML-Parser gcc-c++ gd-devel mod_perl make perl-Net-SSLeay perl-IO-Socket-SSL

echo "Install and configure cpanm"
yes|cpan App::cpanminus

echo "Install Perl libraries (via CPAN)"
cpanm SVN::Hooks --configure-timeout 3600 --build-timeout 3600 --test-timeout 3600
cpanm SVN::Access --configure-timeout 3600 --build-timeout 3600 --test-timeout 3600
cpanm SVN::Access::Group --configure-timeout 3600 --build-timeout 3600 --test-timeout 3600
cpanm XML::Simple --configure-timeout 3600 --build-timeout 3600 --test-timeout 3600
cpanm CGI --configure-timeout 3600 --build-timeout 3600 --test-timeout 3600
cpanm DBI --configure-timeout 3600 --build-timeout 3600 --test-timeout 3600
cpanm DBD::SQLite --configure-timeout 3600 --build-timeout 3600 --test-timeout 3600
cpanm IO::String --configure-timeout 3600 --build-timeout 3600 --test-timeout 3600
cpanm URI::Escape --configure-timeout 3600 --build-timeout 3600 --test-timeout 3600
cpanm parent --configure-timeout 3600 --build-timeout 3600 --test-timeout 3600
cpanm Date::Calc --configure-timeout 3600 --build-timeout 3600 --test-timeout 3600
cpanm GD::Graph --configure-timeout 3600 --build-timeout 3600 --test-timeout 3600
cpanm Time::HiRes --configure-timeout 3600 --build-timeout 3600 --test-timeout 3600
cpanm Lucy --configure-timeout 3600 --build-timeout 3600 --test-timeout 3600
cpanm JSON --configure-timeout 3600 --build-timeout 3600 --test-timeout 3600
cpanm Log::Log4perl --configure-timeout 3600 --build-timeout 3600 --test-timeout 3600
cpanm Log::Dispatch::File --configure-timeout 3600 --build-timeout 3600 --test-timeout 3600
cpanm HTML::Template --configure-timeout 3600 --build-timeout 3600 --test-timeout 3600
cpanm Error --configure-timeout 3600 --build-timeout 3600 --test-timeout 3600
cpanm DateTime --configure-timeout 3600 --build-timeout 3600 --test-timeout 3600
cpanm Archive::Zip --configure-timeout 3600 --build-timeout 3600 --test-timeout 3600
cpanm MIME::Base64 --configure-timeout 3600 --build-timeout 3600 --test-timeout 3600
cpanm Net::SMTP::TLS --configure-timeout 3600 --build-timeout 3600 --test-timeout 3600
cpanm Proc::Fork --configure-timeout 3600 --build-timeout 3600 --test-timeout 3600
cpanm File::Pid --configure-timeout 3600 --build-timeout 3600 --test-timeout 3600
cpanm Proc::Daemon --configure-timeout 3600 --build-timeout 3600 --test-timeout 3600
cpanm Forks::Super --configure-timeout 3600 --build-timeout 3600 --test-timeout 3600
cpanm Math::Round --configure-timeout 3600 --build-timeout 3600 --test-timeout 3600

echo "Setting up directory structure"
mkdir -p /var/db/svn
mkdir -p /var/db/lucy
mkdir -p /var/db/sqlite
mkdir -p /var/log/teamtools
mkdir -p /etc/teamtools

echo "Creating default repository (under /var/db/svn/repo01)"
svnadmin create /var/db/svn/repo01
cp ${TTDIR}/conf/httpd/* /etc/httpd/conf.d/
cp ${TTDIR}/conf/config.xml /etc/teamtools/

echo "Creating empty databases (sqlite3)"
export PERL5LIB=${TTDIR}/lib

chmod 777 ${TTDIR}/bin/*
${TTDIR}/bin/createDepIndex.pl
${TTDIR}/bin/createIndex.pl
${TTDIR}/bin/createMailDB.pl
${TTDIR}/bin/createReviewDB.pl
${TTDIR}/bin/createStatsDB.pl

>/var/log/teamtools/messages

echo "Set permissions (ownership, permissions and SELinux)"
yum -y install policycoreutils-python
chown -R apache:apache /var/db/svn
chown -R apache:apache /var/db/lucy
chown -R apache:apache /var/db/sqlite
chown -R apache:apache /var/log/teamtools
chown -R apache:apache /etc/teamtools
chmod 755 /usr/local/teamtools/bin/*

chcon -R --type httpd_sys_content_t /var/db/svn
chcon -R --type httpd_sys_content_t /var/db/lucy
chcon -R --type httpd_sys_content_t /var/db/sqlite
chcon -R --type httpd_sys_content_t /var/log/teamtools
chcon -R --type httpd_sys_content_t /etc/teamtools
chcon -R --type httpd_sys_content_t /usr/local/teamtools
semanage fcontext -a --type httpd_sys_content_t '/var/db/svn(/*)?'
semanage fcontext -a --type httpd_sys_content_t '/var/db/lucy(/*)?'
semanage fcontext -a --type httpd_sys_content_t '/var/db/sqlite(/*)?'
semanage fcontext -a --type httpd_sys_content_t '/var/log/teamtools(/*)?'
semanage fcontext -a --type httpd_sys_content_t '/usr/local/teamtools(/*)?'

ln -fs ${TTDIR}/web/js /var/www/js
ln -fs ${TTDIR}/web/css /var/www/css
ln -fs ${TTDIR}/web/img /var/www/img

ln -fs ${TTDIR}/bin/svn-hooks.pl /var/db/svn/repo01/hooks/pre-commit
ln -fs ${TTDIR}/bin/svn-hooks.pl /var/db/svn/repo01/hooks/post-commit
